﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace DevelopTools.UI
{
    public class ObjMove : MonoBehaviour
    {
        Vector3 beginPos, endPos;

        public GameObject endObj;
        public MoveState mMoveState;
        public float speed;
        public Transform moveObj;
        Quaternion beginRot, endRot;
        //duibenti xuyao
        public float rotSpeed = 10;
        public bool isDuiBenTi;
        public GameObject rotChild;
        public GameObject[] rotObjs;

        //谱仪
        public bool ifPuYi;

        public Transform moveObjParent, puyiParent;

        public int uiObjIdIndex;

        public GameObject objUIShow;

        //ui button show
        public GameObject uiObjButton;
        //duibenti button
        public GameObject duibentiObjButton;

        //carrUI显示
        public GameObject carrUIShow;

        public GameObject infoFrontShowObj;
        // Use this for initialization
        void Start()
        {
            if (moveObj == null)
                moveObj = transform;

            if (ifPuYi)
            {
                moveObjParent = moveObj.transform.parent.parent.parent;
                puyiParent = moveObj.transform.parent.parent;
            }
            if (objUIShow != null)
            {
                objUIShow.SetActive(false);
            }

            originalPos = moveObj.transform.localPosition;
            originalQua = moveObj.transform.localRotation;

        }

        // Update is called once per frame
        void Update()
        {

            switch (mMoveState)
            {
                case MoveState.moveForward:
                    {
                        percent += Time.deltaTime * speed;
                        percent = Mathf.Min(percent, 1);
                        moveObj.position = Vector3.Lerp(beginPos, endPos, percent);
                        moveObj.transform.rotation = Quaternion.Lerp(beginRot, endRot, percent);
                        if (percent == 1)
                        {
                            mMoveState = MoveState.stayForward;
                            InitRotObj(true);
                            //TextAllControl.instance.SetUIActive(uiObjIdIndex,true);
                            if (objUIShow != null)
                                objUIShow.SetActive(true);

                            UIStateControl.instance.SetObjActive(false);
                            if (ifPuYi)
                            {
                                transform.parent = null;
                                moveObjParent.gameObject.SetActive(false);
                                duibentiObjButton.SetActive(false);
                            }

                            if (isDuiBenTi)
                            {
                                moveObj.transform.gameObject.AddComponent<RotDuiBenTi>();
                            }

                            if (carrUIShow != null)
                                carrUIShow.SetActive(true);
                            uiObjButton.SetActive(true);

                            if (infoFrontShowObj != null)
                                infoFrontShowObj.SetActive(false);

                        }
                    }
                    break;
                case MoveState.moveBack:
                    {
                        percent += Time.deltaTime * speed * 3f;
                        percent = Mathf.Min(percent, 1);

                        moveObj.position = Vector3.Lerp(beginPos, endPos, percent);
                        moveObj.transform.localRotation = Quaternion.Lerp(beginRot, endRot, percent);
                        InitRotObj(false);
                        if (percent == 1)
                        {
                            mMoveState = MoveState.stayBack;
                            InitRotObj(false);
                            //TextAllControl.instance.SetUIAllActiveFalse();
                            if (objUIShow != null)
                                objUIShow.SetActive(false);

                            UIStateControl.instance.SetObjActive(true);

                            moveObj.transform.parent = transform;
                            moveObj.transform.localRotation = Quaternion.identity;
                            moveObj.transform.localPosition = Vector3.zero;
                            if (ifPuYi)
                            {
                                duibentiObjButton.SetActive(true);
                                moveObjParent.gameObject.SetActive(true);
                                transform.parent = puyiParent.transform;
                                UIStateControl.instance.SetObjActive(false);
                            }
                            if (isDuiBenTi)
                            {
                                Destroy(moveObj.gameObject.GetComponent<RotDuiBenTi>());
                                moveObj.transform.localRotation = Quaternion.identity;
                            }

                            uiObjButton.SetActive(false);

                            if (carrUIShow != null)
                                carrUIShow.SetActive(false);
                        }
                    }
                    break;
                case MoveState.stayForward:
                    {
                        if (isDuiBenTi)
                        {
                            rotChild.transform.Rotate(Vector3.up, Time.deltaTime * rotSpeed);
                        }

                    }
                    break;
                case MoveState.stayBack:
                    {

                    }
                    break;
            }

        }

        float percent;

        Vector3 originalPos;
        Quaternion originalQua;

        public void MoveForward()
        {
            if (mMoveState != MoveState.moveForward)
            {
                mMoveState = MoveState.moveForward;
                beginPos = moveObj.transform.position;
                endPos = endObj.transform.position;//Camera.main.transform.position + Camera.main.transform.forward * 1.2f;



                moveObj.transform.parent = null;

                beginRot = moveObj.transform.rotation;
                endRot = endObj.transform.rotation;

                UIStateControl.instance.mStateCarr = UIStateControl.StateCarR.Normal;

                percent = 0;
            }

        }
        public void MoveBack()
        {
            if (mMoveState == MoveState.stayForward || mMoveState == MoveState.moveForward)
            {

                mMoveState = MoveState.moveBack;


                endPos = transform.position;
                beginPos = moveObj.transform.position;

                endRot = transform.rotation;
                beginRot = moveObj.transform.rotation;

                percent = 0;
            }

        }

        void InitRotObj(bool setEnable)
        {
            if (isDuiBenTi)
            {
                for (int i = 0; i < rotObjs.Length; ++i)
                {
                    rotObjs[i].GetComponent<Collider>().enabled = setEnable;
                    rotObjs[i].transform.GetChild(0).GetComponent<Collider>().enabled = setEnable;
                }
            }
        }


        public void SetOriginal()
        {
            if (ifPuYi)
            {
                mMoveState = MoveState.moveBack;
                endPos = moveObj.parent.position;
                beginPos = moveObj.position;

                endRot = beginRot;
                beginRot = moveObj.transform.rotation;

                percent = 1;
            }

        }
    }
    public enum MoveState
    {
        stayBack, moveForward, moveBack, stayForward, movingForward, movingback, none
    }
}